package com.zhousuhang.bingo.activity;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.youmi.android.AdManager;
import net.youmi.android.banner.AdSize;
import net.youmi.android.banner.AdView;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.zhousuhang.bingo.util.MyDialog;

public class MainActivity extends Activity {

	private Spinner spinner;
	private String[] items;
	private ArrayAdapter<String> sAdapter;
	private TextView desc_textView;
	private RatingBar total_ratingBar;
	private RatingBar love_ratingBar;
	private RatingBar work_ratingBar;
	private RatingBar money_ratingBar;
	private TextView health_textView;
	private TextView talk_textView;
	private TextView colour_textView;
	private TextView num_textView;
	private TextView dating_textView;
	private TextView daily_textView;
	private ProgressDialog progressDialog;
	String result = "";
	String now = "";
	// 定义Handler对象
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			try {
				progressDialog.dismiss();  
				JSONObject jsonObject = new JSONObject(result).getJSONObject("xingyun");
				desc_textView.setVisibility(View.VISIBLE);  
				total_ratingBar.setVisibility(View.VISIBLE); 
				
				love_ratingBar.setVisibility(View.VISIBLE);  
				work_ratingBar.setVisibility(View.VISIBLE);  
				money_ratingBar.setVisibility(View.VISIBLE);  
				health_textView.setVisibility(View.VISIBLE);  
				talk_textView.setVisibility(View.VISIBLE);  
				colour_textView.setVisibility(View.VISIBLE);  
				num_textView.setVisibility(View.VISIBLE);  
				dating_textView.setVisibility(View.VISIBLE);  
				daily_textView.setVisibility(View.VISIBLE);  
				
				total_ratingBar.setRating((float) jsonObject.getDouble("total_star"));
				desc_textView.setText("    "+jsonObject.getString("desc"));
				love_ratingBar.setRating((float) jsonObject.getDouble("love_star"));
				work_ratingBar.setRating((float) jsonObject.getDouble("work_star"));
				money_ratingBar.setRating((float) jsonObject.getDouble("money_star"));
				health_textView.setText("  "+jsonObject.getString("health"));
				talk_textView.setText("  "+jsonObject.getString("talk"));
				colour_textView.setText("  "+jsonObject.getString("colour"));
				num_textView.setText("  "+jsonObject.getString("number"));
				dating_textView.setText("  "+jsonObject.getString("dating"));
				daily_textView.setText("  "+jsonObject.getString("date"));
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main);
		//实例化广告条
	    AdView adView = new AdView(this, AdSize.SIZE_320x50);
	    //获取要嵌入广告条的布局
	    LinearLayout adLayout=(LinearLayout)findViewById(R.id.adLayout);
	    //将广告条加入到布局中
	    adLayout.addView(adView);
        
		Date date=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
		now = sf.format(date);
		spinner = (Spinner) this.findViewById(R.id.spinner);
		spinner.setOnItemSelectedListener(new OneItemSelectedListener());
		Resources res = getResources();
		items = res.getStringArray(R.array.listItems);
		sAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, items);
		sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(sAdapter);

		desc_textView = (TextView) this.findViewById(R.id.desc_textView);
		total_ratingBar = (RatingBar)this.findViewById(R.id.total_ratingBar);
		
		love_ratingBar = (RatingBar)this.findViewById(R.id.love_ratingBar);
		work_ratingBar = (RatingBar)this.findViewById(R.id.work_ratingBar);
		money_ratingBar = (RatingBar)this.findViewById(R.id.money_ratingBar);
		health_textView = (TextView)this.findViewById(R.id.health_textView);
		talk_textView = (TextView)this.findViewById(R.id.talk_textView);
		colour_textView = (TextView)this.findViewById(R.id.colour_textView);
		num_textView = (TextView)this.findViewById(R.id.num_textView);
		dating_textView = (TextView)this.findViewById(R.id.dating_textView);
		daily_textView = (TextView)this.findViewById(R.id.daily_textView);
		
	}
	
	class OneItemSelectedListener implements Spinner.OnItemSelectedListener{
		public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
//			Toast.makeText(getApplicationContext(), position+"  "+id,Toast.LENGTH_SHORT).show();
			total_ratingBar.setVisibility(View.INVISIBLE);  
			desc_textView.setVisibility(View.INVISIBLE);  
			love_ratingBar.setVisibility(View.INVISIBLE);  
			work_ratingBar.setVisibility(View.INVISIBLE);  
			money_ratingBar.setVisibility(View.INVISIBLE);  
			health_textView.setVisibility(View.INVISIBLE);  
			talk_textView.setVisibility(View.INVISIBLE);  
			colour_textView.setVisibility(View.INVISIBLE);  
			num_textView.setVisibility(View.INVISIBLE);  
			dating_textView.setVisibility(View.INVISIBLE);  
			daily_textView.setVisibility(View.INVISIBLE);  
			progressDialog = ProgressDialog.show(MainActivity.this, "Loading...", "Please wait...", true, false);
			new GetContentThread(position) .start();
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			
		}
	}

	class GetContentThread extends Thread{
		int position = 0;
		public GetContentThread(int position) {
			this.position = position;
		}
		@Override
		public void run() {
			String astro = "";
			switch (position) {
				case 0:astro="aries";break;
				case 1:astro="taurus";break;
				case 2:astro="gemini";break;
				case 3:astro="cancer";break;
				case 4:astro="leo";break;
				case 5:astro="virgo";break;
				case 6:astro="libra";break;
				case 7:astro="scorpio";break;
				case 8:astro="sagittarius";break;
				case 9:astro="capricorn";break;
				case 10:astro="aquarius";break;
				case 11:astro="pisces";break;
				default:astro="aries";
			}
			
			HttpGet get = new HttpGet("http://www.ttshare.net/astro?con="+astro);
			HttpClient client = new DefaultHttpClient();
			try {
				HttpResponse response = client.execute(get);
				if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					result = EntityUtils.toString(response.getEntity(), "gb2312");
					handler.sendEmptyMessage(0);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override 
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			 MyDialog.dialog(MainActivity.this);
		}
		return true;
	 }
}